package cn.ren.service;

import cn.ren.dao.*;
import cn.ren.entity.PageResult;
import cn.ren.entity.Result;
import cn.ren.pojo.*;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.SimpleDateFormat;
import java.util.*;

@Service(interfaceClass = LZOrderShowService.class)
public class LZOrderShowImpl implements LZOrderShowService {
    @Autowired
    OrderDao orderDao;
    @Autowired
    SetMealDao setMealDao;
    @Autowired
    MemberDao memberDao;
    @Autowired
    LZOrderShowDao lzOrderShowDao;
    @Autowired
    OrderSettingDao orderSettingDao;

    @Override
    public PageResult queryPage(Integer currentPage, Integer pageSize, String queryString) {
        PageHelper.startPage(currentPage, pageSize);
        Page<LZOrderShow> page = lzOrderShowDao.findByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public Result add(LZOrderShow lzOrderShow, int... checkgroupIds) {
        //判断是否是会员
        String phoneNumber = lzOrderShow.getPhoneNumber();
        Member memberByTel = memberDao.findMemberByTel(phoneNumber);
        if (memberByTel == null) {
            //不是就创建会员
            Member member = new Member();
            member.setBirthday(lzOrderShow.getBirthday());
            member.setIdCard(lzOrderShow.getIdCard());
            member.setPhoneNumber(lzOrderShow.getPhoneNumber());
            member.setName(lzOrderShow.getMembername());
            member.setSex(lzOrderShow.getSex());
            member.setRegTime(new Date());
            memberDao.add(member);
            memberByTel = memberDao.findMemberByTel(phoneNumber);
        }
        Date orderDate = lzOrderShow.getOrderDate();


        //比如用户此时勾选了两个 套餐，两个套餐都没预约过，则 预约成功
        //如果用户 勾选的多个套餐中，有其中1个或者多个预约，则需要将 未预约的设置成预约成功，
        String message = "";
        int count = 0;
        if (checkgroupIds != null) {
            for (int checkgroupId : checkgroupIds) {
                OrderSetting orderSetting = orderSettingDao.findOrderSettingByOrderDate(orderDate);
                //预约时间是否合理(orderstting是否设置了该时间，预约人数是已满)
                if (orderSetting == null || orderSetting.getNumber() <= orderSetting.getReservations()) {
                    return new Result(false, "预约失败，请换一天预约");
                }
                List<Order> orderList = orderDao.lzFindBySetmealid(checkgroupId);
                //======
                if (orderList == null || orderList.size() == 0) {
                    Order order1 = new Order();
                    order1.setMemberId(memberByTel.getId());
                    order1.setOrderDate(lzOrderShow.getOrderDate());
                    order1.setOrderStatus("未到诊");
                    order1.setOrderType("微信预约");
                    order1.setSetmealId(checkgroupId);
                    orderDao.add(order1);
                    orderSetting.setReservations(orderSetting.getReservations() + 1);
                    orderSettingDao.editNumberByOrderDate(orderSetting);
                } else {
                    for (Order order : orderList) {
                        count++;
                        if (order.getMemberId() == memberByTel.getId()) {
                            message += (order.getSetmealId() + "号套餐已预约");
                            break;
                        } else if (count == orderList.size()) {
                            Order order1 = new Order();
                            order1.setMemberId(memberByTel.getId());
                            order1.setOrderDate(lzOrderShow.getOrderDate());
                            order1.setOrderStatus("未到诊");
                            order1.setOrderType("微信预约");
                            order1.setSetmealId(checkgroupId);
                            orderDao.add(order1);
                            count = 0;
                            orderSetting.setReservations(orderSetting.getReservations() + 1);
                            orderSettingDao.editNumberByOrderDate(orderSetting);
                            break;
                        }
                    }
                }
            }
        }// 同时告诉用户 哪些已经预约过了
        if (message != null && message.length() > 0) {
            return new Result(true, "预约成功" + message);
        }
      return new Result(true, "预约成功");
    }
}
